﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SimpleCADControl
{
    public partial class LayerPropertiesDialog : Form
    {
        private Dictionary<int, Pen> layerColors;
        public Dictionary<int, Pen> LayerColorTable
        {
            get
            {
                return layerColors;
            }
            set
            {
                layerColors = value;
                ReloadLayers();
            }
        }

        public bool OverrideColors
        {
            get
            {
                return checkBoxOverrideColors.Checked;
            }
            set
            {
                checkBoxOverrideColors.Checked = value;
            }
        }

        public LayerPropertiesDialog()
        {
            InitializeComponent();
        }

        public void ReloadLayers()
        {
            listBoxLayers.Items.Clear();
            foreach (var key in LayerColorTable.Keys)
            {
                listBoxLayers.Items.Add(key);
            }
            try
            {
                listBoxLayers.SelectedIndex = listBoxLayers.Items.Count - 1;
            }
            catch
            {
                //Kein Element vorhanden 
            }

        }

        protected override void OnShown(EventArgs e)
        {
            ReloadLayers();
            base.OnShown(e);
        }

        private void listBoxLayers_SelectedIndexChanged(object sender, EventArgs e)
        {
            numericUpDownWidth.Value = (Decimal) LayerColorTable[(int) listBoxLayers.SelectedItem].Width;
            panelColor.BackColor = LayerColorTable[(int)listBoxLayers.SelectedItem].Color;
        }

        private void numericUpDownWidth_ValueChanged(object sender, EventArgs e)
        {
            LayerColorTable[(int)listBoxLayers.SelectedItem].Width = (float)numericUpDownWidth.Value;
        }

        private void panelColor_Click(object sender, EventArgs e)
        {
            if(colorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                panelColor.BackColor = colorDialog.Color;
                LayerColorTable[(int)listBoxLayers.SelectedItem].Color = colorDialog.Color;
            }
        }

        private void btnAddLayer_Click(object sender, EventArgs e)
        {
            LayerNumber nmb = new LayerNumber();
            nmb.Number = 0;
            if (LayerColorTable.Keys.Count > 0)
            {
                nmb.Number = LayerColorTable.Keys.Max() + 1;
            }
            if (nmb.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (LayerColorTable.Keys.Contains(nmb.Number))
                {
                    MessageBox.Show("Layer bereits vorhanden");
                    return;
                }
                LayerColorTable.Add(nmb.Number, new Pen(Color.Black, 1));
                ReloadLayers();
            }
        }

        private void btnRemoveLayer_Click(object sender, EventArgs e)
        {
            LayerColorTable.Remove((int)listBoxLayers.SelectedItem);
            ReloadLayers();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }
    }
}
